﻿
'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'*******************************************************************





Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling


Imports System.Collections.ObjectModel


Imports System.Data


Imports DC_Rack.Objects.clsDC_RackData


Imports System.ComponentModel
Imports DC_Rack.Objects.dc_Product
Imports System.Xml



Public Class clsSlotMaster


#Region " Properties and declarations "




    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_oc As Collection(Of dc_SlotBase)
    Public m_dc As dc_SlotBase



    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand

    Public Const readAll As String = "selectTblSlotMasterData"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblSlotMasterData

    Public m_parentform As Object
    Property _ParentForm() As Object
        Get
            Return Me.m_parentform
        End Get
        Set(ByVal value As Object)
            Me.m_parentform = value
        End Set
    End Property


    '
    ' within the class endpoints are a collection
    '
    ' in the code it is a collection
    Private m_slotendpointscol As Collection(Of dc_SlotBase.EndPoints)
    Private Property slotEndPointsCol() As Collection(Of dc_SlotBase.EndPoints)
        Get
            If m_slotendpointscol Is Nothing Then
                m_slotendpointscol = New Collection(Of dc_SlotBase.EndPoints)
            End If
            Return Me.m_slotendpointscol
        End Get
        Set(ByVal value As Collection(Of dc_SlotBase.EndPoints))
            If m_slotendpointscol Is Nothing Then
                m_slotendpointscol = New Collection(Of dc_SlotBase.EndPoints)
            End If
            Me.m_slotendpointscol = value
        End Set
    End Property

    Private m_slotendpointsstr As String
    Private Property _slotEndPointStr() As String
        Get
            Return Me.m_slotendpointsstr
        End Get
        Set(ByVal value As String)
            If value Is Nothing Then
                value = "[]"
            End If
            m_slotendpointsstr = value
        End Set
    End Property

    Private m_slotwellformedname As Boolean
    Private Property _slotWellFormedName() As Boolean
        Get
            Return Me.m_slotwellformedname
        End Get
        Set(ByVal value As Boolean)
            Me.m_slotwellformedname = value
        End Set
    End Property

    Private m_slotpairvalid As Boolean




#End Region

#Region " Move/Transform Data "



    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_SlotBase = New dc_SlotBase

        Try
            Dim colslotProductKey As New System.Data.DataColumn
            colslotProductKey.ColumnName = "slotProductKey"
            colslotProductKey.DataType = GetType(Integer)
            colslotProductKey.DefaultValue = newDC.constslotProductKey
            tempTable.Columns.Add(colslotProductKey)

            Dim colslotProductView As New System.Data.DataColumn
            colslotProductView.ColumnName = "slotProductView"
            colslotProductView.DataType = GetType(Int16)
            colslotProductView.DefaultValue = newDC.constslotProductView
            tempTable.Columns.Add(colslotProductView)

            Dim colslotId As New System.Data.DataColumn
            colslotId.ColumnName = "slotId"
            colslotId.DataType = GetType(String)
            colslotId.DefaultValue = newDC.constslotId
            tempTable.Columns.Add(colslotId)

            Dim colslotEndPoints As New System.Data.DataColumn
            colslotEndPoints.ColumnName = "slotEndPoints"
            colslotEndPoints.DataType = GetType(String)
            colslotEndPoints.DefaultValue = newDC.constslotEndPoints
            tempTable.Columns.Add(colslotEndPoints)

            Dim colslotPairValid As New System.Data.DataColumn
            colslotPairValid.ColumnName = "slotPairValid"
            colslotPairValid.DataType = GetType(Boolean)
            colslotPairValid.DefaultValue = newDC.constslotPairValid
            tempTable.Columns.Add(colslotPairValid)

            Dim colslotBuildDate As New System.Data.DataColumn
            colslotBuildDate.ColumnName = "slotBuildDate"
            colslotBuildDate.DataType = GetType(Date)
            colslotBuildDate.DefaultValue = newDC.constslotBuildDate
            tempTable.Columns.Add(colslotBuildDate)

            newDC = Nothing


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If


    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_SlotBase)

        Try

            With tempDC
                tempRow.SetField("slotProductKey", .slotProductKey)
                tempRow.SetField("slotProductView", .slotProductView)
                tempRow.SetField("slotId", .slotId)
                tempRow.SetField("slotEndPoints", .slotEndPoints)
                tempRow.SetField("slotPairValid", .slotPairValid)
                tempRow.SetField("slotBuildDate", .slotBuildDate)
            End With



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .slotProductKey = tempRow.Item("slotProductKey")
                .slotProductView = tempRow.Item("slotProductView")
                .slotId = tempRow.Item("slotId")
                .slotEndPoints = tempRow.Item("slotEndPoints")
                .slotPairValid = tempRow.Item("slotPairValid")
                .slotBuildDate = tempRow.Item("slotBuildDate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_SlotBase)

        Try

            With tempDC
                .initProperties()
                .slotProductKey = tempRow.Item("slotProductKey")
                .slotProductView = tempRow.Item("slotProductView")
                .slotId = tempRow.Item("slotId")
                .slotEndPoints = tempRow.Item("slotEndPoints")
                .slotPairValid = tempRow.Item("slotPairValid")
                .slotBuildDate = tempRow.Item("slotBuildDate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for moving active table to collection for wpf
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_SlotBase)
                Else
                    m_oc = New Collection(Of dc_SlotBase)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_SlotBase
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_SlotBase
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function



#End Region

#Region " Standard Database Calls "

    '
    '
    '
    Private Sub buildInsertSlotMasterCommand()


        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertSlotMaster")
            Dim cmdActive As DbCommand = m_insertCommand

            m_db.AddInParameter(cmdActive, "slotProductKey", DbType.Int32, "slotProductKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotId", DbType.String, "slotId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotProductView", DbType.Int16, "slotProductView", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotEndPoints", DbType.String, "slotEndPoints", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotPairValid", DbType.Boolean, "slotPairValid", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotBuildDate", DbType.Date, "slotBuildDate", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Private Sub buildDeleteSlotMasterCommand()


        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteSlotMaster")
            Dim cmdActive As DbCommand = m_deleteCommand

            m_db.AddInParameter(cmdActive, "slotProductKey", DbType.Int32, "slotProductKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotId", DbType.String, "slotId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotProductView", DbType.Int16, "slotProductView", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildUpdateSlotMasterCommand()


        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateSlotMaster")
            Dim cmdActive As DbCommand = m_updateCommand

            m_db.AddInParameter(cmdActive, "slotProductKey", DbType.Int32, "slotProductKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotId", DbType.String, "slotId", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotProductView", DbType.Int16, "slotProductView", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotEndPoints", DbType.String, "slotEndPoints", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotPairValid", DbType.Boolean, "slotPairValid", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "slotBuildDate", DbType.Date, "slotBuildDate", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblslotmasterdata.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return rowsAffected

    End Function


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addSlotMasterSourceRecord _
            (ByVal tempDC As dc_SlotBase, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblslotmasterdata.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tblslotmasterdata.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    ' clear the general dataset and table, create columns in the table, and move it to
    ' the dataset
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_SlotBase)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_SlotBase)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_SlotBase
            Else
                m_dc = Nothing
                m_dc = New dc_SlotBase
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblslotmasterdata.tostring

            m_ds.Tables.Add(m_dt)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



#End Region

#Region " datbase calls "


    '
    ' deletes the entire table
    '
    Public Sub dbDeleteSlotMasters()


        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("deleteTblSlotMasterData")
            m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' this will be the dataset update method
    '
    Public Sub dbInsertSlotMasterCollection()


        Try

            Dim intX As Integer
            Dim intCt As Integer
            intCt = -1
            Dim itemSlot As dc_SlotBase
            itemSlot = New dc_SlotBase

            If m_oc Is Nothing Then Exit Sub
            intCt = m_oc.Count
            If intCt < 1 Then Exit Sub

            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema
            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            ' Retrieve the initial data
            Dim dbCommand As DbCommand
            dbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(dbCommand, m_ds, dc_racktables.tblslotmasterdata.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblslotmasterdata.tostring)

            Dim m_dr As DataRow = m_dt.NewRow

            For intX = 0 To intCt - 1
                itemSlot = m_oc.Item(intX)
                If Not (itemSlot Is Nothing) Then
                    With itemSlot
                        movePropertiesToDataRow(m_dr, itemSlot)
                        m_dt.Rows.Add(m_dr)
                    End With
                End If
            Next intX

            Dim rowsAffected As Integer
            rowsAffected = dbUpdateDataSet()
            'msgbox rowsAffected



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub dbSelectValidSlotMaster(ByVal lngProdKey As Int32, _
                                       ByVal strSlotId As String, _
                                       ByVal intView As Int16)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectSlotMasterByVendorAndSlotId")
            m_db.AddInParameter(cmd, "@vendorKey", DbType.Int32, lngProdKey)
            m_db.AddInParameter(cmd, "@slotId", DbType.String, strSlotId)
            m_db.AddInParameter(cmd, "@productView", DbType.Int16, intView)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblslotmasterdata.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblslotmasterdata.tostring)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


#End Region

#Region " methods "

    Private colSlots As New Collection()
    Private Sub addSlot()
        Dim newName As String = "-1_-1_Name"
        Dim slotNew As dc_SlotBase = New dc_SlotBase(-1, -1, "name")
        If newName <> "" And containsSlot(newName) = False Then
            colSlots.Add(slotNew, slotNew._slotKey)
        End If
    End Sub
    Private Function containsSlot(ByVal strKey As String) As Boolean
        Dim resultBool As Boolean = False
        If colSlots.Contains(strKey) Then
            resultBool = True
        Else
            resultBool = False
        End If
        Return resultBool
    End Function



#End Region

#Region " Constructor "


    Public Sub New()

        Try

            _ParentForm = Nothing

            m_db = DatabaseFactory.CreateDatabase()
            m_ds = New DataSet
            m_dt = New DataTable

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_SlotBase)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_SlotBase)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_SlotBase
            Else
                m_dc = Nothing
                m_dc = New dc_SlotBase
            End If

            buildDeleteSlotMasterCommand()
            buildInsertSlotMasterCommand()
            buildUpdateSlotMasterCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

#End Region




End Class
